package com.autumn.platform.core.support.model.form;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Form implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7213695598200857194L;
    private String modelId;
    private String title;
    private int columns;
    private String otherConfig;
    private String memo;

    private List<FormField> fields;
    private List<FormField> children; // 直接子节点
    private List<FormField> leaf = null; // 叶子节点
    private String[] properties = null; // 叶子节点对应的数据属性
    private String[] fieldCodes = null; // 叶子节点对应的数据属性
    private Map<String, Integer> indexMap = null;//每个属性下一条件的索引

    /**
     * 构建模型
     * 
     * @param list
     */
    public void build(List<FormField> list) {
        this.setChildren(list);
        this.setLeaf(list);

        List<String> properties = new ArrayList<String>();
        List<String> fieldCodes = new ArrayList<String>();
        Map<String, Integer> indexMap = new HashMap<String, Integer>();
        for (FormField field : this.getLeaf()) {
            String p = field.getProperty();
            properties.add(p);
            fieldCodes.add(field.getFieldCode());
            Integer index = indexMap.get(p);
            if (null == index) {
                index = 0;
            }
            field.setIndex(index);
            indexMap.put(p, ++index);
        }
        this.properties = properties.toArray(new String[properties.size()]);
        this.fieldCodes = fieldCodes.toArray(new String[fieldCodes.size()]);
        this.indexMap = indexMap;
    }

    /**
     * 获取属性的下一条件索引
     * 
     * @param property
     * @return
     */
    public int getPropertyIndex(String property) {
        if (null == indexMap || null == property || null == indexMap.get(property)) {
            return 0;
        } else {
            return indexMap.get(property);
        }
    }

    /**
     * 获取数据属性
     * 
     * @return
     */
    public String[] getDataProperties() {
        return this.properties;
    }

    /**
     * 获取数据属性
     * 
     * @return
     */
    public String[] getDataFieldCodes() {
        return this.fieldCodes;
    }

    public String getModelId() {
        return modelId;
    }

    public void setModelId(String modelId) {
        this.modelId = modelId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getColumns() {
        return columns;
    }

    public void setColumns(int columns) {
        this.columns = columns;
    }

    public String getOtherConfig() {
        return otherConfig;
    }

    public void setOtherConfig(String otherConfig) {
        this.otherConfig = otherConfig;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public List<FormField> getFields() {
        return fields;
    }

    public List<FormField> getChildren() {
        return children;
    }

    public List<FormField> getLeaf() {
        return leaf;
    }

    /**
     * 设置子节点
     * 
     * @param list
     */
    private void setChildren(List<FormField> list) {
        int size = list.size();
        List<FormField> orderList = new ArrayList<FormField>(); // 排好序的Td节点
        List<FormField> children = new ArrayList<FormField>(); // 排好序的Td节点
        List<Integer> parentSeqno = new ArrayList<Integer>();
        for (int i = 0; i < size; i++) {
            FormField field = list.get(i);
            if (field.getFieldSeqno() > 0) {
                parentSeqno.add(field.getFieldSeqno());
            }
        }

        for (int i = 0; i < size; i++) {
            FormField field = list.get(i);
            if (0 == field.getParentFieldSeqno() || !parentSeqno.contains(field.getParentFieldSeqno())) {
                orderList.add(field);
                children.add(field);
                setChildren(list, orderList, field);
            }
        }
        this.fields = orderList;
        this.children = children;
    }

    /**
     * 设置子节点
     * 
     * @param list
     * @param orderList
     * @param parent
     */
    private void setChildren(List<FormField> list, List<FormField> orderList, FormField parent) {
        for (int i = 0, size = list.size(); i < size; i++) {
            FormField field = list.get(i);
            if (field.getParentFieldSeqno() == parent.getFieldSeqno()) {
                parent.addChild(field);
                orderList.add(field);
                setChildren(list, orderList, field);
            }
        }
    }

    /**
     * 设置叶子节点
     */
    private void setLeaf(List<FormField> list) {
        List<FormField> children = new ArrayList<FormField>();
        for (int i = 0, size = list.size(); i < size; i++) {
            FormField td = list.get(i);
            if (td.isLeaf()) {
                children.add(td);
            }
        }
        this.leaf = children;
    }
}
